package controller;

/*
Cours :             LOG120 
Session :           Saison (hiver) 
Groupe :            01 
Projet :            Laboratoire #2
Etudiant(e)(s) :    Jeremy Moreau
                    Alexandre Richard
                    Philippe Pigeon
Code(s) perm. :     MORJ91010106
					RICA10028806 
                    PIGP08058903
Professeur :        Ghizlane El Boussaidi  
Date remise :       2012-03-010  
*/

import db.DataClass;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import model.Commande;
import model.LigneCommandeRestaurant;
import view.CommandeViewer;
import view.CommandesPretesView;
import view.GoogleMapView;

/**
 *
 * @author aj91000
 */
public class LivraisonController extends AbstractController implements ActionListener
{
	private JFrame mainFrame;
	
	private CommandesPretesView cpView;
	private GoogleMapView gmView;
	private CommandeViewer cViewer;
	
	private Commande observedCommande;
	
	private String currentLocation;
	private JMenuItem btLocation;
	
	public LivraisonController()
	{
		cpView = new CommandesPretesView(this);
		gmView = new GoogleMapView(this);	
		cViewer = new CommandeViewer();
		
		initMainFrame();
		
		askCurrentLocation();
	}
	
	private void initMainFrame()
	{
		//frame
		mainFrame = new JFrame("Livraison Pretes");
		
		//menu
		JMenuBar menuBar = new JMenuBar();
		mainFrame.setJMenuBar(menuBar);
		
		JMenu mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		btLocation = new JMenuItem("Changer location");
		btLocation.addActionListener(this);
		mnFichier.add(btLocation);
		
		JMenuItem mntmLogout = new JMenuItem("Logout");
		mntmLogout.setAction(new LogoutAction(mainFrame));
		mnFichier.add(mntmLogout);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mntmQuitter.setAction(new QuitterAction());
		mnFichier.add(mntmQuitter);
		
		//legende
		JLabel label = new JLabel("<html><b>Legende</b><br><font color='green'>Vert</font>: Depart<br> <font color='red'>Rouge</font>: Arrivee <br><font color='blue'>Bleu</font>: Restaurants </html>"); 
		label.setBounds(10, 530, 110, 130);
		mainFrame.getContentPane().add(label);
		
		//panels
		cpView.setBounds(10, 10, 180, 400);
		cViewer.setBounds(10, 370, 180, 180);
		gmView.setBounds(210, 10, gmView.MAP_WIDTH, gmView.MAP_HEIGHT);
		mainFrame.getContentPane().add(cpView);
		mainFrame.getContentPane().add(cViewer);
		mainFrame.getContentPane().add(gmView);
		
		//options
		mainFrame.setResizable(false);
		mainFrame.setBounds(100, 100, 860, 705);
		mainFrame.getContentPane();
		mainFrame.setLayout(null);
		mainFrame.setVisible(true);
	}

	public void updateGoogleMapView(Commande _commande) 
	{
		if(_commande == null) return;
		_commande.deleteObservers();
		_commande.addObserver(gmView);
		_commande.refresh();
		cViewer.update(_commande);
		observedCommande = _commande;
	}
	
	public String getCurrentLocation()
	{
		return currentLocation;
	}
	
	public void askCurrentLocation()
	{
		currentLocation = JOptionPane.showInputDialog("Entrez votre location courante: ", "1106 Notre-Dame Ouest, Montreal, QC");
		if(currentLocation!= null)
			currentLocation = currentLocation.replace(" ", "%20");
		cpView.clearSelection();
	}

	public void setAccepted(int idConfirmation) 
	{
		System.out.println("Commande " + idConfirmation + " acceptee.");
		String restoString = "Restaurants:\n";
		for(LigneCommandeRestaurant lcr : observedCommande.getLignesCommandeRestaurant())
		{
			restoString += lcr.getRestaurant().getNom() + " : " + lcr.getRestaurant().getAdresse() +"\n";
		}
		System.out.println(restoString);
		DataClass.getCarnetDeCommande().setDelivered(idConfirmation);	
	}

	@Override
	public void actionPerformed(ActionEvent arg0) 
	{
		if(arg0.getSource() == btLocation)
		{
			askCurrentLocation();
		}
		
	}


}
